<?php


namespace App\Services;


use App\Repositories\TariffRepository;

class TariffService extends BaseService
{

    protected $repository;

    public function __construct()
    {
        $this->repository = new TariffRepository();
    }


    /**
     * 删除资费
     * @param int $id 资费ID
     * @return bool
     * @Author 姿势就是力量
     */
    public function delete(int $id)
    {
        $tariff = $this->repository->findBy('id', $id, ['service_type_id', 'type', 'company_id']);
        if (!$tariff) return $this->setError('未找到该资费信息');

        // 当业务类型只剩1个时，不可删除
        $count = $this->repository->getCount([
            'service_type_id' => $tariff->service_type_id,
            'type' => $tariff->type,
            'company_id' => $tariff->company_id
        ]);
        if ($count == 1) {
            return $this->setError('该公司当前业务类型只剩此条资费，不可删除');
        }
        $res = $this->repository->destroy($id);
        if (!$res) return $this->setError('删除失败');

        return true;
    }

}